/**
 * gecodemodel.cpp is part of Gedule.
 *
 * Gedule is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Gedule is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Gedule.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "gecodemodel.h"
#include "../gedulemgr.h"
#include "../model/model.h"
#include "../model/task.h"
#include "../model/resource.h"
#include "../model/constraints/task_contraints/lenght.h"
#include "../model/constraints/task_contraints/neededresources.h"
#include "../model/constraints/task_contraints/timewindow.h"
#include "../model/constraints/resource_contraints/capacity.h"
#include "../model/constraints/resource_contraints/unavailabilities.h"
#include "../model/constraints/model_contraints/allowidles.h"
#include "../model/constraints/model_contraints/maxtime.h"
#include "../model/constraints/model_contraints/precedence.h"
#include "../utils/logging/logmanager.h"
#include <gecode/search.hh>


///////////////////////////////////////////////////////////////
GecodeModel::GecodeModel( Model & a_model ) :
    Gecode::Space(),
    Visitor(),
    Printable()
{
    a_model.Accept( this );
    InitVars();
    PostConstraints();
}

///////////////////////////////////////////////////////////////
GecodeModel::GecodeModel( bool a_share, GecodeModel & a_other ) :
    Space( a_share, a_other ),
    Visitor(),
    Printable()
{
}

///////////////////////////////////////////////////////////////
GecodeModel::~GecodeModel()
{
}

///////////////////////////////////////////////////////////////
GecodeModel * GecodeModel::copy( bool a_share )
{
    return new GecodeModel( a_share, *this );
}

///////////////////////////////////////////////////////////////
std::string GecodeModel::Print() const
{
    return "";
}

///////////////////////////////////////////////////////////////
void GecodeModel::Visit( Model * a_model )
{
}

///////////////////////////////////////////////////////////////
void GecodeModel::Visit( TimeWindow * a_constraint )
{
    if( a_constraint )
    {
        m_startTimesArgs << Gecode::IntVar( *this, a_constraint->Start(), a_constraint->End() );
        m_endTimesArgs << Gecode::IntVar( *this, a_constraint->Start(), a_constraint->End() );
    }
}

///////////////////////////////////////////////////////////////
void GecodeModel::Visit( Lenght * a_constraint )
{
    if( a_constraint )
        m_lenghts << a_constraint->GetLenght();
}

///////////////////////////////////////////////////////////////
void GecodeModel::Visit( NeededResources * a_constraint )
{
    if( a_constraint )
        m_resourcesNeeded << a_constraint->GetNeededResources();
}

///////////////////////////////////////////////////////////////
void GecodeModel::Visit( Capacity * a_constraint )
{
    if( a_constraint )
        m_capacities << a_constraint->GetCapacity();
}

///////////////////////////////////////////////////////////////
void GecodeModel::Visit( Unavailabilities * a_constraint )
{
    if( a_constraint )
    {

    }
}

///////////////////////////////////////////////////////////////
void GecodeModel::Visit( AllowIdles * a_constraint )
{
    m_allowIdles = a_constraint ? a_constraint->IsAllowIdles() : false;
}

///////////////////////////////////////////////////////////////
void GecodeModel::Visit( MaxTime * a_constraint )
{
    m_maxTime = a_constraint ? a_constraint->GetMaxTime() : MAX_TIME;
}

///////////////////////////////////////////////////////////////
void GecodeModel::Visit( Task * a_task )
{
    if( a_task )
        m_tasksNames.push_back( a_task->TaskName() );
}

///////////////////////////////////////////////////////////////
void GecodeModel::Visit( Resource * a_resource )
{
    if( a_resource )
        m_resourcesNames.push_back( a_resource->ResourceName() );
}

///////////////////////////////////////////////////////////////
void GecodeModel::InitVars()
{
    m_startTimesVars = Gecode::IntVarArray( *this, m_startTimesArgs );
    m_endTimesVars   = Gecode::IntVarArray( *this, m_endTimesArgs );
}

///////////////////////////////////////////////////////////////
void GecodeModel::PostConstraints()
{

}

///////////////////////////////////////////////////////////////
bool GecodeModel::Solve()
{
    bool ret = true;
    std::ostringstream strStream;
    try
    {
        strStream << "---- Searching solutions started ----";
        GET_LOG_MGR().Log( LOG_LOGGER, strStream.str() );
        strStream.str( std::string() );

        unsigned short iSolution = 0;
        Gecode::DFS< GecodeModel > searchEngine( this );
        while( GecodeModel * solution = searchEngine.next() )
        {
            if( solution )
            {
                strStream << "Solution " << iSolution << ": " << std::endl << solution->Print();
                GET_LOG_MGR().Log( LOG_LOGGER, strStream.str() );
                strStream.str( std::string() );

                delete solution;
                ++iSolution;
            }
        }

        strStream << "---- Searching solutions finished ----";
        GET_LOG_MGR().Log( LOG_LOGGER, strStream.str() );
        strStream.str( std::string() );
    }
    catch( std::exception & exception )
    {
        strStream << "Gecode exception while searching solutions : " << exception.what();
        GET_LOG_MGR().Log( LOG_LOGGER, strStream.str(), ERROR_LOG );
        ret = false;
    }

    return ret;
}

